'''
@文件    :views.py
@说明    :
@时间    :2020/04/05 19:07:05
@作者    :didiplus
@版本    :1.0
'''
from flask import Blueprint,request,make_response,json,jsonify
from .__init__ import *

video = Blueprint('video',__name__,url_prefix='/api/video')

@video.route('/url',methods=['POST','GET'])
def url():
    # 获取视频URL
    query = request_query(request, "id", ["res", {"resolution": 1080}])
    query["ids"] = '["{}"]'.format(query.pop("id"))
    data = Send(query).POST("weapi/cloudvideo/playurl")
    return make_response(jsonify(json.loads(data.text)),403)

@video.route('/detail',methods=['POST','GET'])
def detail():
    # 获取视频详情
    query = request_query(request, "id")
    data = Send(query).POST("weapi/cloudvideo/v1/video/detail")
    return make_response(jsonify(json.loads(data.text)),403)

@video.route('/lists',methods=['POST','GET'])
def lists():
    # 获取视频标签列表
    data = Send().POST("weapi/cloudvideo/group/list")
    return make_response(jsonify(json.loads(data.text)),403)

@video.route('/group',methods=['POST','GET'])
def group():
    # 获取视频标签下的视频
    query = request_query(request,
                          ["id", "groupId"],
                          ["offset", {"offset": 0}],
                          ["res", {"resolution": 1080}])
    query["needUrl"] = True
    data = Send(query).POST("weapi/videotimeline/videogroup/get")
    return make_response(jsonify(json.loads(data.text)),403)

@video.route('/related',methods=['POST','GET'])
def related():
    # 相关视频
    query = request_query(request, "id")
    query["type"] = 0 if query["id"].isdigit() else 1
    data = Send(query).POST("weapi/cloudvideo/v1/allvideo/rcmd")
    return make_response(jsonify(json.loads(data.text)),403)

@video.route('/sub',methods=['POST','GET'])
def sub():
    # 收藏与取消收藏视频
    query = request_query(request, "id", ["t", {"t": 1}])
    t = "sub" if query.pop("t") == "1" else "unsub"
    data = Send(query).POST("weapi/cloudvideo/video/" + t)
    return make_response(jsonify(json.loads(data.text)),403)